package com.c39.otc.open.api.users;

import com.c39.cloud.rbac.facade.StaffFacade;
import com.c39.cloud.rbac.model.auth.AuthStaffVO;
import com.c39.cloud.rbac.model.staff.StaffDetailsDTO;
import com.c39.cloud.rbac.model.staff.StaffVO;
import com.c39.cloud.rbac.model.userAuth.DataPowerV2DTO;
import com.c39.cloud.rbac.model.userAuth.UserAuthVO;
import com.c39.otc.open.api.users.response.UserInfosResponse;
import com.cqfan.cloud.framework.base.Result;
import com.cqfan.cloud.framework.redis.client.RedisFacade;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户模块
 * @author JiaLing.FanX
 * @version 1.0
 * @description: TODO
 * @date 2025/9/10 16:19
 *
 */
@RestController
@RequestMapping("/v1/users")
public class UserController {


    @Resource
    private RedisFacade redisFacade;

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private StaffFacade staffFacade;

    /**
     * 获取用户信息(所有员工信息)
     * @param empNum 员工编号
     * @return
     * @throws Exception
     */
    @GetMapping("/getUserInfoByEmpNum/{empNum}")
    public Result<UserInfosResponse> userInfoByEmpNum(@PathVariable("empNum") String empNum) throws Exception {
        return Result.fail("暂未开放");
    }

    /**
     * 获取用户信息(登录用户)
     * @param userName 用户账号:LDAP账号
     * @return
     * @throws Exception
     */
    @GetMapping("/getUserInfoByUserName/{userName}")
    public Result<UserInfosResponse> userInfoByUserName(@PathVariable("userName") String userName) throws Exception {

        StaffDetailsDTO staffDetailsDTO = new StaffDetailsDTO();
        staffDetailsDTO.setStaffName(userName);
        Result<StaffVO> result =staffFacade.detailsByStaffName(staffDetailsDTO);
        if(!result.getCode().equals("0")){
            return Result.fail("用户不存在");
        }

        StaffVO staffVO = result.getData();
        String content = redisFacade.get("cr999.staff.rbac.sid." + staffVO.getStaffNumber());
        if(StringUtils.isEmpty(content)){
            return Result.fail("用户不存在");
        }
        AuthStaffVO authStaffVO =  objectMapper.readValue(content, AuthStaffVO.class);
        UserInfosResponse userInfosResponse = new UserInfosResponse();
        userInfosResponse.setStaffSex(authStaffVO.getStaffSex());
        userInfosResponse.setStaffEmail(authStaffVO.getStaffEmail());
        userInfosResponse.setOrgName(authStaffVO.getOrgName());
        userInfosResponse.setRealName(authStaffVO.getRealName());
        userInfosResponse.setStaffName(authStaffVO.getStaffName());
        userInfosResponse.setStaffNumber(authStaffVO.getStaffNumber());
        userInfosResponse.setStaffPhone(authStaffVO.getStaffPhone());
        userInfosResponse.setRoleNames(authStaffVO.getRoleNames());
        userInfosResponse.setLowerEmpNames(authStaffVO.getLowerEmpNames());
        userInfosResponse.setLowerEmpNumbers(authStaffVO.getLowerEmpNumbers());
        userInfosResponse.setDirectLowerEmpNumbers(authStaffVO.getDirectLowerEmpNumbers());
        DataPowerV2DTO dataPowerV2DTO = new DataPowerV2DTO();
        dataPowerV2DTO.setStaffNumber(authStaffVO.getStaffNumber());
        Map<String, List<UserAuthVO>> userAuths = authStaffVO.getUserPermissions();
        List<UserAuthVO> userAuthVOList = userAuths.get("10259");
        userInfosResponse.setMenus(userAuthVOList);
        return Result.success(userInfosResponse);
    }
}
